﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class AreaBLL : IBaseInfo
    {
        private static readonly IAreaDA areaDA = DataAccess.CreateAreaDA();
        private static readonly IAreaDetailDA areaDetailDA = DataAccess.CreateAreaDetailDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public AreaBLL()
        {
            this.pubParasInfo = null;
        }

        public AreaBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public AreaBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> areaIDS)
        {
            string temp = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<AreaInfo> araeInfos = new List<AreaInfo>();
                AreaInfo areaInfo = null;
                foreach (string id in areaIDS)
                {
                    areaInfo = areaDA.GetAreaByID(dataBase, tran, id, this.dataBaseID[0]);
                    areaInfo.AreaDetailInfos = areaDetailDA.GetAreaDetailByAreaID(dataBase, tran, id, this.dataBaseID[0]);
                    araeInfos.Add(areaInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                areaDetailDA.DeleteAreaDetail(dataBase, tran, areaIDS, this.dataBaseID);
                areaDA.DeleteArea(dataBase, tran, areaIDS, this.dataBaseID);
                for (int i = 0; i < areaIDS.Count; i++)
                {
                    auditDataBLL.InsertAuditData<AreaInfo>(dataBase, tran, araeInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "AREA", this.dataBaseID[0]);
                    if ((araeInfos[i].AreaDetailInfos != null) && (araeInfos[i].AreaDetailInfos.Count > 0))
                    {
                        for (int j = 0; j < araeInfos[i].AreaDetailInfos.Count; j++)
                        {
                            auditDataBLL.InsertAuditData<AreaDetailInfo>(dataBase, tran, araeInfos[i].AreaDetailInfos[j], null, AuditAction.Delete, this.pubParasInfo.UserID, "AREA_DETAIL", this.dataBaseID[0]);
                        }
                    }
                    temp = temp + areaIDS[i] + ",";
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            if (temp.Length > 0)
            {
                return temp.Substring(0, temp.Length - 1);
            }
            return "";
        }

        public DataSet GetAllInfo()
        {
            return areaDA.GetAllArea(this.dataBaseID[0]);
        }

        public object GetInfoByID(string areaID)
        {
            AreaInfo area = areaDA.GetAreaByID(areaID, this.dataBaseID[0]);
            if (area != null)
            {
                area.AreaDetailInfos = areaDetailDA.GetAreaDetailByAreaID(areaID, this.dataBaseID[0]);
            }
            return area;
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            AreaQueryEntity areaQuery = obj as AreaQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(areaQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + areaQuery.IsActive);
            }
            if (!string.IsNullOrEmpty(areaQuery.AreaID))
            {
                sqlWhere.Add(" AREA_ID " + areaQuery.AreaID);
            }
            if (!string.IsNullOrEmpty(areaQuery.Descr))
            {
                sqlWhere.Add(" DESCR " + areaQuery.Descr);
            }
            return areaDA.GetAreaByQueryList(sqlWhere, areaQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            string areaID = string.Empty;
            try
            {
                AreaInfo area = obj as AreaInfo;
                if (areaDA.CheckAreaIDUnique(dataBase, tran, area.AreaID, this.dataBaseID[0]))
                {
                    PublicMethod.GetInstance().ShowAlertMessage("F2340_001", this.pubParasInfo.UserLanguage, new List<string> { area.AreaID });
                }
                area.UpdatedBy = area.CreatedBy;
                area.CreatedDate = DateTime.Now;
                area.UpdatedDate = DateTime.Now;
                areaDA.InsertArea(dataBase, tran, area, this.dataBaseID);
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                auditDataBLL.InsertAuditData<AreaInfo>(dataBase, tran, null, area, AuditAction.Insert, this.pubParasInfo.UserID, "AREA", this.dataBaseID[0]);
                areaID = area.AreaID;
                if ((area.AreaDetailInfos != null) && (area.AreaDetailInfos.Count > 0))
                {
                    for (int i = 0; i < area.AreaDetailInfos.Count; i++)
                    {
                        if (areaDetailDA.CheckAreaDetailIDUnique(dataBase, tran, area.AreaDetailInfos[i].AreaID, area.AreaDetailInfos[i].ZoneID, this.dataBaseID[0]))
                        {
                            PublicMethod.GetInstance().ShowAlertMessage("F2340_002", this.pubParasInfo.UserLanguage, new List<string> { area.AreaDetailInfos[i].AreaID, area.AreaDetailInfos[i].ZoneID });
                        }
                        else
                        {
                            area.AreaDetailInfos[i].CreatedBy = area.CreatedBy;
                            area.AreaDetailInfos[i].UpdatedBy = area.UpdatedBy;
                            area.AreaDetailInfos[i].CreatedDate = DateTime.Now;
                            area.AreaDetailInfos[i].UpdatedDate = DateTime.Now;
                            areaDetailDA.InsertAreaDetail(dataBase, tran, area.AreaDetailInfos[i], this.dataBaseID);
                            auditDataBLL.InsertAuditData<AreaDetailInfo>(dataBase, tran, null, area.AreaDetailInfos[i], AuditAction.Insert, this.pubParasInfo.UserID, "AREA_DETAIL", this.dataBaseID[0]);
                        }
                    }
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return areaID;
        }

        public string Update(object obj)
        {
            string areaID = string.Empty;
            try
            {
                AreaInfo area = obj as AreaInfo;
                area.UpdatedDate = DateTime.Now;
                area.UpdatedBy = this.pubParasInfo.UserID;
                AreaInfo oldAreaInfo = areaDA.GetAreaByID(area.AreaID, this.dataBaseID[0]);
                if (areaDA.UpdateArea(area, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<AreaInfo>(oldAreaInfo, area, AuditAction.Update, this.pubParasInfo.UserID, "AREA", this.dataBaseID[0]);
                    areaID = area.AreaID;
                }
            }
            catch (Exception ex)
            {
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            return areaID;
        }
    }
}

